#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctime>

using namespace std;
int count = 0;
int add = 0;
void handler(int signo)
{
    pid_t rid;
    while (rid = waitpid(-1, 0, WNOHANG) > 0)
    {
        printf("catch a signo: %d\n", signo);
        sleep(10);
        count++;
        printf("%d已回收\n", count);
    }
}

int main()
{
    srand(time(0));
    signal(17, handler);

    for (int i = 0; i < 5; i++)
    {
        pid_t id = fork();
        add++;
         if (id == 0)
        {
            printf("%d已创建\n",add);
            sleep(10);
            printf("clild quit\n");
            exit(0);
        }
    }
       
    while (true)
    {
        sleep(1);
    }
}